//
//  IntrodeceView.m
//  jingjing2.0
//
//  Created by apple on 16/5/6.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "IntrodeceView.h"
#import "JianXiData.h"
#import "IntroduceData.h"
#import "IntroduceTableViewCell.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width/2.0
#define COLOR [UIColor colorWithRed:217/255.0 green:217/255.0 blue:217/255.0 alpha:0.3]
@implementation IntrodeceView
{
    UIButton *_countPrice;
    UIButton *_serviceProcess;
    UIView *_HorLine;
    UIView *_verLine;
    NSInteger _selectedBtn;
    NSInteger _verSelectedBtn;
    UILabel *_label;
    UIScrollView *_scrollView;
}
-(id)initWithArr:(NSMutableArray *)arr andFrame:(CGRect)rect
{
    if (self=[super init]) {
        self.frame=rect;
        [self setUpCustomSelectedControl];
        [self setUpVerCustomSelectedControlWithArr:arr];
        [self setUpHorLine];
        [self setUpVerLine];
        _dataSource=[NSMutableArray array];
    }
    return self;
}
#pragma mark--创建横向选择项
-(void)setUpCustomSelectedControl
{
        for (int i=0; i<2; i++) {
        UIButton *selected=[[UIButton alloc]initWithFrame:CGRectMake(i*WIDTH, 0, WIDTH, 30)];
        [selected setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        selected.tag=(i+1)*100;
        selected.titleLabel.font=[UIFont systemFontOfSize:15];

        if (i==0) {
            [selected setTitle:@"计价" forState:UIControlStateNormal];
            _selectedBtn=selected.tag;
            [selected setTitleColor:BLUECOLOR forState:UIControlStateNormal];
        }else
        {
            [selected setTitle:@"服务流程" forState:UIControlStateNormal];
 
        }
        [selected addTarget:self action:@selector(clickOnSeleced:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:selected];
    }
}
-(void)clickOnSeleced:(UIButton *)btn
{
    if (_selectedBtn==btn.tag) {
        return;
    }
    if (btn.tag==200) {
        _scrollView.contentOffset=CGPointMake(self.frame.size.width, 0);
    }else
    {
        _scrollView.contentOffset=CGPointMake(0, 0);
    }
    UIButton *button=[self viewWithTag:_selectedBtn];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [btn setTitleColor:BLUECOLOR forState:UIControlStateNormal];
    
    [UIView animateWithDuration:0.1 animations:^{
        _HorLine.frame=CGRectMake(btn.frame.origin.x, btn.frame.origin.y+30, WIDTH, 2);
 
    }];
    _selectedBtn=btn.tag;
}
#pragma mark--创建竖向选择项和横向滑动视图
-(void)setUpVerCustomSelectedControlWithArr:(NSMutableArray *)arr
{
    //下方可以滑动的视图
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, self.frame.size.width, self.frame.size.height-30)];
    _scrollView.contentSize=CGSizeMake(2*self.frame.size.width, 0);
    _scrollView.pagingEnabled=YES;
    _scrollView.showsVerticalScrollIndicator=NO;
    _scrollView.showsHorizontalScrollIndicator=NO;
    [self addSubview:_scrollView];
    
    for (int i=0; i<2;i++ ) {
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(i*_scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        
        view.tag=(i+1)*1000;
        view.backgroundColor=[UIColor whiteColor];
        view.layer.borderColor=COLOR.CGColor;
        view.layer.borderWidth=1.0;
        [_scrollView addSubview:view];
    }
   
    UIView *view=[_scrollView viewWithTag:1000];
    UIView *webView=[_scrollView viewWithTag:2000];
    //计价
      for (int i=0; i<arr.count; i++) {
        JianXiData *data=arr[i];
        CustomButton *btn=[[CustomButton alloc]initWithFrame:CGRectMake(0, i*80, 80, 80)];
        [btn setTitle:[data.ProOrderCategoryName stringByRemovingPercentEncoding] forState:UIControlStateNormal];
          btn.titleLabel.font=[UIFont systemFontOfSize:15];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickOnVerSelectedCtl:) forControlEvents:UIControlEventTouchUpInside];
          btn.code=[NSString stringWithFormat:@"%@",data.ProOrderCategoryCode];
          btn.tag=(i+1)*10;
        btn.backgroundColor=COLOR;
        if (i==0) {
            [btn setTitleColor:BLUECOLOR forState:UIControlStateNormal];
            btn.backgroundColor=[UIColor whiteColor];
            _verSelectedBtn=btn.tag;
        }
        [view addSubview:btn];
    }
    
     JianXiData *data=arr[0];
    _label=[[UILabel alloc]initWithFrame:CGRectMake(90, 0, view.frame.size.width-80, 30)];
    _label.text=[NSString stringWithFormat:@"洗%@价格表",[data.ProOrderCategoryName stringByRemovingPercentEncoding]];
    _label.backgroundColor=COLOR;
    _label.textColor=[UIColor lightGrayColor];
    _label.font=[UIFont systemFontOfSize:13];
    [view addSubview:_label];
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(80, _label.frame.origin.y+30, view.frame.size.width-80, view.frame.size.height-30)];
    [_tableView registerNib:[UINib nibWithNibName:@"IntroduceTableViewCell" bundle:nil] forCellReuseIdentifier:@"reuse"];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=100;
    [view addSubview:_tableView];
    [self getRequestDataWithProCatCode:data.ProOrderCategoryCode];
    
    //服务流程
    UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, webView.frame.size.width, webView.frame.size.height)];
    NSURL *url=[NSURL URLWithString:SERVICE_PROCESS];
    NSURLRequest *request=[[NSURLRequest alloc] initWithURL:url];
    web.opaque = NO;
    webView.backgroundColor = [UIColor clearColor];
    web.scalesPageToFit=YES;
    [web loadRequest:request];
    [webView addSubview:web];
}
-(void)clickOnVerSelectedCtl:(CustomButton *)btn
{
    if (_verSelectedBtn==btn.tag) {
        return;
    }
    UIButton *button=[self viewWithTag:_verSelectedBtn];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.backgroundColor=COLOR;
    [UIView animateWithDuration:0.1 animations:^{
        _verLine.frame=CGRectMake(btn.frame.origin.x, btn.frame.origin.y+30,2, 80);
    }];
    NSString *title=[btn titleForState:UIControlStateNormal];
    _label.text=[NSString stringWithFormat:@"洗%@价格表",title];
    btn.backgroundColor=[UIColor whiteColor];
    [btn setTitleColor:BLUECOLOR forState:UIControlStateNormal];
    
    [self getRequestDataWithProCatCode:btn.code];
    _verSelectedBtn=btn.tag;
}
#pragma mark--表代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    IntroduceTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    IntroduceData *data=self.dataSource[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:data.ImageUrl]];
    cell.name.text=[data.ItemName stringByRemovingPercentEncoding];
    cell.price.text=[NSString stringWithFormat:@"￥%@元/%@",data.RealPrice,data.ComputeUnitName];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark--创建指示线
//横向
-(void)setUpHorLine
{
    _HorLine=[[UIView alloc]initWithFrame:CGRectMake(0, 30, WIDTH, 2)];
    _HorLine.backgroundColor=BLUECOLOR;
    [self addSubview:_HorLine];
}
//竖直
-(void)setUpVerLine
{
   _verLine=[[UIView alloc]initWithFrame:CGRectMake(0, 30, 2, 50)];
    _verLine.backgroundColor=BLUECOLOR;
    [self addSubview:_verLine];
}
#pragma mark--获取数据
-(void)getRequestDataWithProCatCode:(NSString *)code
{
    [MBProgressHUD showHUDAddedTo:_tableView animated:YES];
    NSString *timestamp=[GeneralTool gettimestamp];
    NSDictionary *dic=@{@"ProCatCode":code,
                        @"Timestamp":timestamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"Product/ProList/%@/%@/%@",code,timestamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"请求数据失败，请检查网络～"];
        }else
        {
            if ([responses[@"IsSuccess"]intValue]==1) {
                [MBProgressHUD hideHUDForView:_tableView];
                NSArray *arr=responses[@"ReturnObject"];
                [self.dataSource removeAllObjects];
                for (NSDictionary *dic in arr) {
                    IntroduceData *data=[[IntroduceData alloc]initWithDic:dic];
                    [self.dataSource addObject:data];
                    [_tableView reloadData];
                }
            }
        }
    }];
    
}
@end
